from typing import List
import itertools
import operator

def remove_redundant_parentheses(expression: str) -> str:
    """
    Remove one pair of redundant parentheses from the expression if possible.
    
    Args:
        expression: The input arithmetic expression
        
    Returns:
        The expression with one pair of redundant parentheses removed if found,
        otherwise returns the original expression.
    """
    # If no parentheses, return the expression as is
    if '(' not in expression and ')' not in expression:
        return expression
    
    # Find all pairs of parentheses
    stack = []
    pairs = []
    
    for i, char in enumerate(expression):
        if char == '(':
            stack.append(i)
        elif char == ')':
            if stack:
                start = stack.pop()
                pairs.append((start, i))
    
    # Check each pair of parentheses
    for start, end in pairs:
        # Remove this pair of parentheses
        new_expr = expression[:start] + expression[start+1:end] + expression[end+1:]
        
        # Check if the new expression is valid and evaluates to the same result
        try:
            # Remove spaces and evaluate both expressions
            original_value = eval(expression.replace(' ', ''))
            new_value = eval(new_expr.replace(' ', ''))
            
            # If the new expression is valid and evaluates to the same result, return it
            if abs(new_value - original_value) < 1e-10:
                return new_expr
        except:
            continue
    
    # If no redundant parentheses found, return the original expression
    return expression

def calc_all_expressions(nums: List[int]) -> List[str]:
    """
    Find all valid arithmetic expressions using exactly the four given numbers
    that evaluate to 24, then remove redundant parentheses from each expression.
    
    Args:
        nums: A list of four natural numbers between 1 and 13
        
    Returns:
        A list of strings representing valid arithmetic expressions that
        evaluate to 24 with no redundant parentheses.
    """
    # Get the initial list of expressions
    expressions = calc_24_points(nums)
    
    # Remove redundant parentheses from each expression
    cleaned_expressions = []
    for expr in expressions:
        # Continuously remove redundant parentheses until none are left
        current_expr = expr
        while True:
            new_expr = remove_redundant_parentheses(current_expr)
            if new_expr == current_expr:
                break
            current_expr = new_expr
        
        # Add to the result list
        if current_expr not in cleaned_expressions:
            cleaned_expressions.append(current_expr)
    
    return cleaned_expressions

# Test code
def calc_24_points(nums: List[int]) -> List[str]:
    # Store all valid expressions
    valid_expressions = []
    
    # Define arithmetic operations
    operations = {
        '+': operator.add,
        '-': operator.sub,
        '*': operator.mul,
        '/': lambda x, y: x / y if y != 0 else float('inf')  # Avoid division by zero
    }
    
    # Define all possible ways to combine four numbers with parentheses
    def evaluate(nums_order, ops):
        # Try all possible ways to parenthesize the expression and evaluate it
        results = []
        a, b, c, d = nums_order
        op1, op2, op3 = ops
        
        # Case 1: ((a op b) op c) op d
        try:
            val1 = operations[op1](a, b)
            val2 = operations[op2](val1, c)
            val3 = operations[op3](val2, d)
            expr1 = f"(({a} {op1} {b}) {op2} {c}) {op3} {d}"
            results.append((expr1, val3))
        except:
            pass
        
        # Case 2: (a op (b op c)) op d
        try:
            val1 = operations[op2](b, c)
            val2 = operations[op1](a, val1)
            val3 = operations[op3](val2, d)
            expr2 = f"({a} {op1} ({b} {op2} {c})) {op3} {d}"
            results.append((expr2, val3))
        except:
            pass
        
        # Case 3: a op ((b op c) op d)
        try:
            val1 = operations[op2](b, c)
            val2 = operations[op3](val1, d)
            val3 = operations[op1](a, val2)
            expr3 = f"{a} {op1} (({b} {op2} {c}) {op3} {d})"
            results.append((expr3, val3))
        except:
            pass
        
        # Case 4: a op (b op (c op d))
        try:
            val1 = operations[op3](c, d)
            val2 = operations[op2](b, val1)
            val3 = operations[op1](a, val2)
            expr4 = f"{a} {op1} ({b} {op2} ({c} {op3} {d}))"
            results.append((expr4, val3))
        except:
            pass
        
        # Case 5: (a op b) op (c op d)
        try:
            val1 = operations[op1](a, b)
            val2 = operations[op3](c, d)
            val3 = operations[op2](val1, val2)
            expr5 = f"({a} {op1} {b}) {op2} ({c} {op3} {d})"
            results.append((expr5, val3))
        except:
            pass
        
        return results
    
    # Try all permutations of the input numbers
    for nums_perm in itertools.permutations(nums):
        # Try all possible combinations of operations
        for ops in itertools.product(operations.keys(), repeat=3):
            # Evaluate all possible expressions
            expressions = evaluate(nums_perm, ops)
            
            # Check if any of the expressions evaluates to 24 (within a small tolerance)
            for expr, value in expressions:
                if abs(value - 24) < 1e-10:  # Use small tolerance for floating point comparison
                    # Format the expression to ensure proper spacing
                    formatted_expr = expr.replace('+', ' + ').replace('-', ' - ').replace('*', ' * ').replace('/', ' / ')
                    formatted_expr = ' '.join(formatted_expr.split())  # Remove extra whitespace
                    if formatted_expr not in valid_expressions:
                        valid_expressions.append(formatted_expr)
    
    return valid_expressions

if __name__ == "__main__":
    test_nums = [7, 11, 3, 10]
    results = calc_all_expressions(test_nums)
    
    print(f"Found {len(results)} expressions that evaluate to 24:")
    for expr in results:
        try:
            # Evaluate the expression to verify
            result = eval(expr.replace(' ', ''))
            print(f"{expr} = {result}")
        except:
            print(f"{expr} = Error evaluating")